package com.pwt.service;

import com.pwt.entity.Hierarchy;
import com.pwt.entity.MASCHINE;
import com.pwt.model.HierarchyConfigModel;
import com.pwt.model.TreeModel;
import com.pwt.repository.HierarchyRepository;
import com.pwt.repository.MASCHINERepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Hierarchy service层
 *
 * @author auto generated
 * @date 2021-10-08 11:30:48
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HierarchyService {


    private HierarchyRepository hierarchyRepository;

    private MASCHINERepository maschineRepository;

    public HierarchyService(HierarchyRepository hierarchyRepository, MASCHINERepository maschineRepository) {
        this.hierarchyRepository = hierarchyRepository;
        this.maschineRepository = maschineRepository;
    }

    public void updateHierarchy(List<Hierarchy> list){
        if(list == null || list.size() < 1 ) return;
        this.hierarchyRepository.deleteByAbilityLineIdIsNull();
        updateHierarchy(list,null);

    }



    public void updateHierarchy(List<Hierarchy> list,String pid){
        if(list == null || list.size() < 1) return;
        for(Hierarchy he:list){
            if(pid != null && !"".equals(pid.trim())){
                he.setPid(pid);
                he.setId(null);
            }
            Hierarchy save = this.hierarchyRepository.save(he);
            if(he.getChildren() != null && he.getChildren().size() > 0){
                updateHierarchy(he.getChildren(),save.getId());
            }
        }
    }


    public HierarchyConfigModel queryHierarchy(){
        List<MASCHINE> list = this.maschineRepository.queryMaschine();
        List<Hierarchy> hierarchyList = this.hierarchyRepository.findAllNotInAbilityLineId();
        List<Hierarchy> result = new ArrayList<>();
        for (Hierarchy h:hierarchyList){
            if(h.getPid() == null){
                h.setChildren(queryTree(h,hierarchyList));
                result.add(h);
            }
        }
        HierarchyConfigModel configModel = new HierarchyConfigModel();
        configModel.setHierarchies(result);
        configModel.setMachines(list);
        return configModel;
    }

    /**
     * 获取树形层级
     * @param hierarchyList
     * @return
     */
    public List<Hierarchy> queryHierarchyOnTree(List<Hierarchy> hierarchyList){
        List<Hierarchy> result = new ArrayList<>();
        for (Hierarchy h:hierarchyList){
            if(h.getPid() == null){
                h.setChildren(queryTree(h,hierarchyList));
                result.add(h);
            }
        }
        return result;
    }

    public List<Hierarchy> queryTree(Hierarchy ph,List<Hierarchy> hierarchyList){
        List<Hierarchy> childList = queryHierarchyListById(ph,hierarchyList);
        if(childList != null && childList.size() >0){
            for (Hierarchy h:childList){
                h.setChildren(queryTree(h,hierarchyList));
            }
        }
        return childList;
    }

    public List<Hierarchy> queryHierarchyListById(Hierarchy h,List<Hierarchy> hierarchyList){
        List<Hierarchy> resultList = new ArrayList<>();
        for (Hierarchy child:hierarchyList){
            if(h.getId().equals(child.getPid())){
                resultList.add(child);
            }
        }
        return resultList;
    }
}
